package cc.rengu.redp.flowable.bizmodel.web.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.utils.RedpUtil;
import cc.rengu.redp.flowable.bizmodel.BizModelNormalBuilder;
import cc.rengu.redp.flowable.bizmodel.metadata.ModelHandler;
import cc.rengu.redp.flowable.bizmodel.db.domain.BizData;
import cc.rengu.redp.flowable.bizmodel.domain.BizDataQueryConditions;
import cc.rengu.redp.flowable.bizmodel.web.service.BizCfgModelService;
import cc.rengu.redp.flowable.bizmodel.web.service.BizDataService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务模型管理
 * @author xushitang
 */
@Slf4j
@RestController
@RequestMapping("/bizData")
public class BizDataController extends BaseController {

    @Autowired
    private ModelHandler modelHandler;
    @Autowired
    private BizCfgModelService bizCfgModelService;
    @Autowired
    private BizDataService bizDataService;

    @Log("业务模型数据查询")
    @GetMapping("/query")
    public RedpResponse getBizData(QueryRequest queryRequest, String modelId, String[] conditions) {
        if (!modelHandler.modelAuth(bizCfgModelService.getBaseMapper().selectById(modelId), RedpUtil.getCurrentUser())) {
            return RedpResponse.getInstance().failure().message("无权访问该模型数据");
        }

        List<BizDataQueryConditions> conList = new ArrayList<>();
        if (conditions.length != 0){
            for (String con : conditions){
                String[] conSplit = con.split("@@@");
                if (conSplit.length != 3){
                    return RedpResponse.getInstance().failure().message("搜索条件异常");
                }

                conList.add(new BizDataQueryConditions(conSplit[0], conSplit[1], conSplit[2]));
            }
        }

        IPage<BizData> ipBizData =  bizDataService.findBizDataList(queryRequest, modelId, conList);
        if (ipBizData == null) {
            return RedpResponse.getInstance().failure().message("获取业务数据失败");
        }
        //解构BizData，只取data部分
        IPage<String> ipString =  new Page<>();
        List<String> dataStringList = new ArrayList<>();
        ipBizData.getRecords().forEach(bizData -> {
            JSONObject jsonObject = JSON.parseObject(bizData.getData());
            jsonObject.put(BizModelNormalBuilder.RESERVE_FIELD_KEY, bizData.getDataId());
            jsonObject.put(bizData.getUniqKeyName(), bizData.getUniqKey());
            dataStringList.add(jsonObject.toJSONString());
        });
        ipString.setRecords(dataStringList);
        ipString.setCurrent(ipBizData.getCurrent());
        ipString.setSize(ipBizData.getSize());
        ipString.setPages(ipBizData.getPages());
        ipString.setTotal(ipBizData.getTotal());
        return RedpResponse.getInstance().data(getDataTable(ipString)).success();
    }
}
